package com.sotb.sender;

import java.io.File;
import java.util.Calendar;

import android.media.MediaRecorder;
import android.media.MediaRecorder.OutputFormat;
import android.os.Environment;

/**
 * 
 * @author lgb
 * 录音类
 */
public class Recorder {
	private static String SDCardPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator;
	private static String SubPatb = "SOTB" ;
	private static MediaRecorder recorder;
	private static File audioFile = null; 
	private static File audioDir = null;
	private static boolean SDcardExit = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
	
	private static String strTempFile;
	public static Calendar myCalendar;
	public static int myYear;
	public static int myMonth;
	public static int myDay;
    public static int myWeekday;
    public static int myHour;
    public static int myMinute;
    public static int mySecond;
	
	public static void startRecord(){
		//检测SD卡是否存在
		if (SDcardExit){
			try{
				recorder = new MediaRecorder();
				//设置录音源
				recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
				//设置输出格式
				recorder.setOutputFormat(OutputFormat.THREE_GPP);
				//设置编码
				recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
				//创建文件夹
				audioDir = new File(SDCardPath + SubPatb + File.separator);
				audioDir.mkdirs();
				//创建输出文件
				time();
				audioFile = new File(audioDir, strTempFile + ".amr");
				//设置输出路径
				recorder.setOutputFile(audioFile.getAbsolutePath());
				//开始录音		
				recorder.prepare();
				recorder.start();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		else{
			System.out.println("SDcard not Exit");
		}
	}
	
	public static void stopRecord() {
		if (recorder != null){
			recorder.stop();
			recorder.release();
			recorder = null;
		}
    }
	
	private static String time()
	  {
	  //取得系统时间
	  long time = System.currentTimeMillis();
	  //通过Calendar对象取得小时和分钟
	  myCalendar = Calendar.getInstance();
	  myCalendar.setTimeInMillis(time);
	  myYear = myCalendar.get(Calendar.YEAR);
	  myMonth = myCalendar.get(Calendar.MONTH);
	  myDay = myCalendar.get(Calendar.DAY_OF_MONTH);
	  myWeekday = myCalendar.get(Calendar.DAY_OF_WEEK_IN_MONTH);
	  myHour = myCalendar.get(Calendar.HOUR);
	  myMinute = myCalendar.get(Calendar.MINUTE);
	  mySecond = myCalendar.get(Calendar.SECOND);
	  
	  strTempFile = myYear + "-" + myMonth + "-" + myDay + "_" + myHour + "-" +  myMinute + "-" + mySecond;
	  return strTempFile;
	  }

}